﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Jemgine.Engine
{
    public class EntityComponent : Data.ScriptNode, Data.EntityElement, Data.MapElement
    {
        [Microsoft.Xna.Framework.Content.ContentSerializerIgnore, 
#if !XBOX
        System.ComponentModel.Browsable(false)
#endif
        ]
        public Data.EntityRecord Me { get; set; }

        [ContentSerializer(Optional = true)]
        public int BoneID { get; set; }
        [ContentSerializerIgnore]
        public Data.Bone BoneReference { get; set; }

        public Vector2 GetAnimatedWorldPosition()
        {
            return Vector2.Transform(Position, GetFullTransform());
        }

        public Matrix GetFullTransform()
        {
            var M = Me == null ? Matrix.Identity : Me.GetTransform();
            if (BoneReference != null) M = BoneReference.Transform * M;
            return M;
        }

        public float GetAnimatedAngle(float A)
        {
            var M = GetFullTransform();
            var Up = new Vector2(0, 1);
            Up = Vector2.Transform(Up, Matrix.CreateRotationZ(A));
            var AnimatedWorldPos = Vector2.Transform(Position, M);
            var AnimatedAnglePos = Vector2.Transform(Position + Up, M);
            return Math.Vector.AngleBetweenVectors(new Vector2(0, 1), AnimatedAnglePos - AnimatedWorldPos);
        }

        protected override Vector2 _getWorldPosition()
        {
            if (Me == null) return Position;
            else return Me.Position + Position;
        }

        public virtual void _Initialize(ContentManager Content) { }
        public virtual void _Destroy() { }

        public void SetOwner(Data.EntityRecord E)
        {
            Me = E;
        }

        #region EntityElement Members

        public bool PointInside(Microsoft.Xna.Framework.Vector2 point)
        {
            Vector2 Dimensions = GetDimensions();
            Vector2 ItemPosition = Position - (Dimensions / 2);

            if (point.X >= ItemPosition.X && point.X < ItemPosition.X + Dimensions.X &&
                point.Y >= ItemPosition.Y && point.Y < ItemPosition.Y + Dimensions.Y) return true;

            return false;
        }

        new public Jemgine.Engine.Selection.Types SelectionType()
        {
            return Jemgine.Engine.Selection.Types.EntityElement;
            //return Jemgine.Engine.Selection.Types.Component;
        }

        Jemgine.Data.EntityElement Jemgine.Data.EntityElement.Clone()
        {
            return (Jemgine.Data.EntityElement)base.Clone();
        }

        public void Initialize(Data.EntityRecord Self, Microsoft.Xna.Framework.Content.ContentManager Content)
        {
            Me = Self;
            _Initialize(Content);
        }

        public void Destroy()
        {
            _Destroy();
        }

        #endregion

        public virtual void Reset(EntityComponent To)
        {
            var KeepMe = Me;
            CopyProperties(To);
            Me = KeepMe;
        }
    }
}
